home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / gilhooly / mainwind.c < prev   
C/C++ Source or Header  |  1995-09-10  |  4KB  |  140 lines

  1. ---------------- MAINWND.C    -- (Window Procedure) ----------------
  2. #include "pulse.h"
  3. /*
  4.     Routine:    MainWndProc
  5.     Called By:  Windows
  6.     Usage:      This is the window procedure for the main window
  7. */
  8.  
  9. long FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  10. {
  11.  
  12.        char         szBuffer[BUFMAX+1];
  13.            char             szTitle[BUFMAX+1];
  14. static DWORD        dwIn = 0,
  15.                     dwOut = 0;
  16. static DWORD        MaxTicks = 0,
  17.                     MinTicks = 32767;
  18.        DWORD        Ticks;
  19.        HDC          hDC;
  20.        HMENU        hPulseMenu;
  21.        PAINTSTRUCT  ps;
  22.        POINT        ptClick;
  23.        RECT         rect;
  24.  
  25.     switch (message)
  26.     {
  27. /*
  28. read INI file, reset window position to previous, floating over all other, start clock
  29. */
  30.       case WM_CREATE:
  31.         GetIni();
  32.             SetWindowPos(hWnd, HWND_TOPMOST, nXPos, nYPos, 100, 50, 0);
  33.         SetTimer(hWnd, ID_TIMER, (UINT)1000, NULL);
  34.         break;
  35. /*
  36. save the last position (for restoring on restart)
  37. */
  38.       case WM_MOVE:
  39.         nXPos = LOWORD(lParam);
  40.         nYPos = HIWORD(lParam);
  41.         wsprintf(szBuffer, "%d", nXPos);
  42.         WriteProfileString("Pulse", "XPos", szBuffer);
  43.         wsprintf(szBuffer, "%d", nYPos);
  44.         WriteProfileString("Pulse", "YPos", szBuffer);
  45.         break;
  46. /*
  47. all we do on the timer is redraw our contents
  48. */
  49.       case WM_TIMER:
  50.         InvalidateRect(hWnd, NULL, TRUE);
  51.         break;
  52.   /*
  53.   RMB creates context menu - selection processed in WM_COMMAND
  54.   */
  55.       case WM_RBUTTONDOWN:
  56.         hPulseMenu = CreatePopupMenu();
  57.         InsertMenu(hPulseMenu, 0, MF_BYPOSITION, MI_PULSE,    "&Current");
  58.         InsertMenu(hPulseMenu, 1, MF_BYPOSITION, MI_MAXPULSE, "Ma&x Pulse");
  59.         InsertMenu(hPulseMenu, 2, MF_BYPOSITION, MI_MINPULSE, "Mi&n Pulse");
  60.         ptClick = MAKEPOINT(lParam);
  61.         ClientToScreen(hWnd, &ptClick);
  62.         TrackPopupMenu(hPulseMenu, TPM_LEFTALIGN, ptClick.x, ptClick.y, 0, hWnd, NULL);
  63.         InvalidateRect(hWnd, NULL, TRUE);
  64.         DestroyMenu(hPulseMenu);
  65.         break;
  66.  
  67.       case WM_COMMAND:
  68.          Mode = wParam;
  69.          wsprintf(szBuffer, "%d", Mode);
  70.          WriteProfileString("Pulse", "Mode", szBuffer);
  71.          break;
  72.  
  73.       case WM_PAINT:
  74.         dwIn = GetTickCount();
  75.         hDC = BeginPaint(hWnd, &ps);
  76.         GetClientRect(hWnd, &rect);
  77.         if (dwOut)
  78.         {
  79.           SetTextColor(hDC, RGB(0, 0, 255));
  80.           Ticks = dwIn - dwOut;
  81.           switch (Mode)
  82.           {
  83.             case MI_PULSE:
  84.               lstrcpy(szTitle, (LPSTR)"Pulse");
  85.               wsprintf(szBuffer, "%lu Ticks", (DWORD)Ticks);
  86.               break;
  87.  
  88.             case MI_MAXPULSE:
  89.               lstrcpy(szTitle, (LPSTR)"Max Pulse");
  90.               wsprintf(szBuffer, "%lu Ticks", (DWORD)MaxTicks);
  91.               break;
  92.  
  93.             case MI_MINPULSE:
  94.               lstrcpy(szTitle, (LPSTR)"Min Pulse");
  95.               wsprintf(szBuffer, "%lu Ticks", (DWORD)MinTicks);
  96.               break;
  97.           }
  98. /*
  99. we draw the text centered in the client area
  100. */
  101.           GetClientRect(hWnd, &rect);
  102.           DrawText(hDC, szBuffer, -1,  &rect,  DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  103.           SetWindowText(hWnd, szTitle);
  104. /*
  105. reset global counters if necessary
  106. */
  107.           if (Ticks > MaxTicks)
  108.             MaxTicks = Ticks;
  109.           if (Ticks < MinTicks)
  110.             MinTicks = Ticks;
  111.         }
  112.         EndPaint(hWnd, &ps);
  113.         dwOut = GetTickCount();
  114.         break;
  115. /*
  116. pressing the home key emulates clicking the RMB
  117. */
  118.       case WM_KEYDOWN:
  119.         if (wParam == VK_HOME)
  120.           PostMessage(hWnd, WM_RBUTTONDOWN, (WPARAM)0, MAKELPARAM(0, 0));
  121.         break;
  122.  
  123.       case WM_CLOSE:
  124.         PutIni();
  125.         DestroyWindow(hWnd);
  126.         break;
  127.  
  128.       case WM_DESTROY:
  129.         PostQuitMessage(0);
  130.         break;
  131.  
  132.       default:
  133.         return (DefWindowProc(hWnd, message, wParam, lParam));
  134.         break;
  135.     }
  136.  
  137.     return (NULL);
  138. }
  139.  
  140.